<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>部门管理</el-breadcrumb-item>
      <el-breadcrumb-item>部门列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-form
      :inline="true"
      :model="form"
      label-width="80px"
      @submit.native.prevent
    >
      <el-form-item label="查询部门">
        <el-input
          prefix-icon="el-icon-search"
          @keyup.native.enter="queryDept"
          v-model="form.deptId"
          placeholder="请输入部门ID"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="queryDept" type="primary" round>搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dept" style="width: 100%">
      <el-table-column label="部门ID" prop="deptId"></el-table-column>
      <el-table-column label="部门名称" prop="deptName"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="delDept(scope.row.deptId)"
            circle
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-view"
            circle
          ></el-button>
          <el-button
            type="success"
            size="small"
            @click="modifyDept(scope.row.deptId)"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            type="warning"
            size="small"
            icon="el-icon-cold-drink"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="pageChange"
      background
      :page-size="pagesize"
      :current-page="page"
      layout="->,prev, pager, next"
      :total="sum"
    >
    </el-pagination>

    <el-dialog title="修改部门" :visible.sync="dialogFormVisible">
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="dialogFormVisible = false"
          >取 消</el-button
        >
        <el-button type="primary" @click="confirmModify">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      form: {
        deptId: "",
        deptName: "",
      },
      dept: [],
      pagesize: 6,
      page: 1,
      sum: 0,
      rules: {
        deptName: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    //加载部门列表  分页查询
    let params = { page: this.page, pagesize: this.pagesize };
    this.$http.deptApi
      .pageList(params)
      .then((res) => {
        //console.log(res);
        this.dept = res.data.data[0];
        this.sum = res.data.data[1][0].sum;
      })
      .catch((err) => console.log(err));
  },
  methods: {
    // 通过id查部门
    queryDept() {
      if (this.form.deptId) {
        this.$http.deptApi
          .queryById(this.form.deptId)
          .then((res) => {
            console.log(res);
            this.dept = res.data.data;
          })
          .catch((err) => console.log(err));
      } else {
        this.loadDept();
      }
    },
    // 加载部门列表
    loadDept() {
      //加载部门列表  分页查询
      let params = { page: this.page, pagesize: this.pagesize };
      this.$http.deptApi
        .pageList(params)
        .then((res) => {
          //console.log(res);
          this.dept = res.data.data[0];
          this.sum = res.data.data[1][0].sum;
        })
        .catch((err) => console.log(err));
    },
    //删除部门
    delDept(bid) {
      this.$http.deptApi
        .delete({ deptId: bid })
        .then((res) => {
          console.log(res);
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: "删除部门成功",
            });
            this.loadDept();
          }
        })
        .catch((err) => console.log(err));
    },
    // 页码改变时，列表也变化
    pageChange(page) {
      //console.log(page);
      this.page = page;
      let params = { page, pagesize: this.pagesize };
      this.$http.deptApi
        .pageList(params)
        .then((res) => {
          //console.log(res);
          this.dept = res.data.data[0];
          this.sum = res.data.data[1][0].sum;
        })
        .catch((err) => console.log(err));
    },

    // 修改分店
    modifyDept(id) {
      this.dialogFormVisible = true;
      this.$store.commit("setDeptId", id);
      //console.log(this.$store.state.branchId);
    },

    // 确认修改
    confirmModify() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let params = {
            deptId: this.$store.state.deptId,
            deptName: this.form.deptName,
          };
          this.$http.deptApi
            .modify(params)
            .then((res) => {
              //console.log(res);
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "修改部门成功！",
                });
                this.$refs["form"].resetFields();
                this.dialogFormVisible = false;
                this.loadDept();
              }
            })
            .catch((err) => console.log(err));
        } else {
          this.$message({
            type: "error",
            message: "表单填写错误！",
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
<style lang="scss">
.el-dialog__title {
  color: #409eff;
  font-size: 27px;
  font-weight: bold;
}
.el-form-item__label {
  color: orangered;
}
</style>